package com.xlf.common.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.xlf.cache.CatalogList;
import com.xlf.cache.Option;
import com.xlf.cache.PlanList;
import com.xlf.common.interceptor.MaterialInterceptor;
import com.xlf.common.model.Factory;
import com.xlf.common.model.Product;
import com.xlf.common.model.User;
import com.xlf.mail.MailProperties;
import com.xlf.mail.SendMail;
import com.xlf.web.SessionKey;

@Before(MaterialInterceptor.class)
public class ProductController extends Controller {

	@ClearInterceptor
	public void login() {
		renderJsp("login.jsp");
	}
	
	public void index() {
		
		User user = getSessionAttr(SessionKey.USER);
		int userType = user.getInt("type");
		// 页面显示主材列表用
		List<Option> catalogList = CatalogList.getList();
		setAttr("catalogList", catalogList);
		
		String catalogId = getPara("catalogId");
		if(catalogId == null) {
			catalogId = "1";
		}
		
		setAttr("planList", PlanList.getList());
		
		String brand = getPara("brand");
		String type = getPara("type");
		String status = getPara("status");
		if(userType == 11){ // 主材专员 不能查看供货价格innerPrice
			setAttr("userType", userType);
		}else{
			setAttr("userType", "10");
		}
		// 组织查询数据用于页面回显查询条件
		setAttr("catalogId", catalogId);
		setAttr("type", type);
		setAttr("brand", brand);
		setAttr("status", status);

		Page<Product> pageRecord = Product.dao.exeSearchSql(getParaToInt("page", 1), 20, catalogId, type, brand, status, null, null);
		setAttr("pageRecord", pageRecord);
		renderJsp("index.jsp");
	}

	public void save() {
		UploadFile file = getFile("img");
		//System.out.println(file.getSaveDirectory());
		
		Product product = getModel(Product.class, "product");
		if(file != null){
			product.set("pic", file.getFileName());
		}
		
		String notice = null;
		if (product.get("p_id") == null) {
			product.save();
			notice = "商品信息添加成功";
		} else if (product.getInt("p_id") > 0) {
			product.update();
			notice = "商品信息更新成功";
		}
		
		setAttr("notice", notice);
		// 继续添加商品避免重复设置属性
		setAttr("catalogId", product.get("c_id"));
		setAttr("factoryId", product.get("f_id"));
		setAttr("planId", product.get("planType"));
		
		renderJsp("product_res.jsp");
		//forwardAction("index");
		//index();
	}

	public void form() {
		Integer id = getParaToInt("id");
		if (id != null && id > 0) {
			Product product = Product.dao.findById(id);
			setAttr("product", product);
			setAttr("catalogId", product.getInt("c_id"));
			
			List<Option> catalogList = CatalogList.getList();
			setAttr("catalogList", catalogList);
			
			List<Option> planList = PlanList.getList();
			setAttr("planList", planList);
			
			List<Factory> factoryList = Factory.dao.find("select f_id, name from factory where c_id=?", product.getInt("c_id"));
			setAttr("factoryList", factoryList);
		}
		
		renderJsp("form.jsp");
	}
	
	public void delete(){
		Integer id = getParaToInt("id");
		String notice = "删除商品操作失败";
		if (id != null && id > 0) {
			boolean flag =  Product.dao.deleteById(id);
			if (flag) {
				notice = "删除商品操作成功";
			}
		} 
		setAttr("notice", notice);
		
		index();
	}
	
	public void addNew(){
		String catalogIdStr = getPara("catalogId");
		int cid = 1;
		if(catalogIdStr != null) {
			cid = Integer.parseInt(catalogIdStr);
		}
		setAttr("catalogId", cid);
		setAttr("factoryId", getPara("factoryId"));
		setAttr("planId", getPara("planId"));
		
		List<Option> catalogList = CatalogList.getList();
		setAttr("catalogList", catalogList);
		List<Option> planList = PlanList.getList();
		setAttr("planList", planList);
		
		List<Factory> factoryList = Factory.dao.find("select f_id, name from factory where c_id=?", cid);
		setAttr("factoryList", factoryList);
		
		renderJsp("form.jsp");
	}

	public void mail(){
		setAttr("mailserver",MailProperties.getMailserver());
		setAttr("username", MailProperties.getUsername());
		setAttr("password", MailProperties.getPassword());
		setAttr("receive", MailProperties.getReceive());
		setAttr("send", MailProperties.getSend());
		setAttr("mailtitle", MailProperties.getMailtitle());
		setAttr("mailcontent", MailProperties.getMailcontent());
		setAttr("isstart", MailProperties.getIsstart()=="on"?"on":"off");
		renderJsp("mail.jsp");
	}
	public void send(){
		String mailserver = getPara("mailserver");
		String username = getPara("username");
		String password = getPara("password");
		String receive = getPara("receive");
		String send = getPara("send");
		String mailtitle = getPara("mailtitle");
		String mailcontent = getPara("mailcontent");
		String isstart = getPara("isstart");
		SendMail sendMail = new SendMail();
		sendMail.setSmtpServer(mailserver);
		// 此处设置登录的用户名
		sendMail.setUsername(username);
		// 此处设置登录的密码
		sendMail.setPassword(password);
		// 设置收件人的地址
		sendMail.setTo(receive);
		// 设置发送人地址
		sendMail.setFrom(send);
		// 设置标题
		sendMail.setSubject(mailtitle);
		// 设置内容
		sendMail.setContent(mailcontent);
		sendMail.setIsstart(isstart==null?"off":"on");
		
		MailProperties.saveProperties(sendMail);
//		
//		if (sendMail.send()) {
//			System.out.println("---邮件发送成功---");
//		}
		mail();
	}
}
